package com.project.service;

import com.project.dao.DepartmentMapper;
import com.project.entity.Department;
import com.project.service.base.BaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Created by WheelChen on 2017/5/4.
 */
@Transactional(rollbackFor=Exception.class)
@Service
public class DepartmentServiceImpl extends BaseService implements DepartmentService {
    private static DepartmentMapper departmentMapper=null;

    public void setMapper()
    {
        if(departmentMapper==null)
            departmentMapper = sqlSessionTemplate.getMapper(DepartmentMapper.class);//1.获取相关mapper
    }

    @Override
    public int addOne(Department department) {
        setMapper();
        return departmentMapper.insertSelective(department);
    }

    @Override
    public int remove(Integer id) {
        setMapper();
        return departmentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int modify(Department department) {
        setMapper();
        return departmentMapper.updateByPrimaryKeySelective(department);
    }

    @Override
    public Department findOne(Integer id) {
        setMapper();
        return departmentMapper.selectByPrimaryKey(id);
    }

    /**
     * author: WheelChen
     * date: 2015-5-4
     * function: 根据schoolId 查找系/部/科室
     *
     * @param schoolId
     * @return
     */
    @Override
    public List<Department> findBatchBySchoolId(Integer schoolId) {
        setMapper();
        return departmentMapper.selectBySchoolId(schoolId);
    }

    /**
     * author: WheelChen
     * date: 2017/8/3
     * function: 根据学院id查找学院下的系-专业-班级
     *
     * @param schoolId
     * @return
     */
    @Override
    public List<Department> findDetailBatch(Integer schoolId) {
        setMapper();
        return departmentMapper.selectDetailBatch(schoolId);
    }

    /**
     * author: WheelChen
     * date: 2017/8/10
     * function: 检查系是否已存在
     *
     * @param department
     * @return true - 存在 false - 不存在
     */
    @Override
    public boolean checkMajorIsExist(Department department) {
        setMapper();
        return departmentMapper.checkMajorIsExist(department) != 0;
    }

    /**
     * author: WheelChen
     * date: 2017/10/9
     * function: 根据条件分页模糊查找系
     *
     * @param department
     * @return
     */
    @Override
    public List<Department> findBatchByCondition(Department department) {
        setMapper();
        return departmentMapper.selectBatchByCondition(department);
    }
}
